<?php
namespace app\api\controller;
use think\App;
use think\Controller;
use app\api\logic\User as UserLogic;
use app\api\logic\Posts as PostsLogic;
use think\facade\Session;
use think\validate\ValidateRule;

class User extends Controller
{
    protected $failException = true;

    private $userLogic;
    private $postsLogic;
    public function __construct(App $app = null,UserLogic $userLogic,PostsLogic $postsLogic)
    {
        parent::__construct($app);
        $this->userLogic=$userLogic;
        $this->postsLogic=$postsLogic;
    }

    public function login()
    {
        $data=$this->request->param();
        if($data['type']==2){
            $this->validate($data,'app\api\validate\User.login_2');
        }else{
            $this->validate($data,'app\api\validate\User.login_1');
        }
        $result=$this->userLogic->login($data);
        if($result['req']==1){
            if($result['scene']==1){
                $userInfo=$this->getResponseInfo($result);
                $userInfo['scene']=$result['scene'];
                return json(['data'=>$userInfo,'code'=>ERRNO['OK'],'msg'=>'ok']);
            }else{
                //去注册
                return json(['data'=>['scene'=>2],'code'=>ERRNO['OK'],'msg'=>'ok']);
                //return json(['data'=>['result'=>intval($result['id']),'scene'=>$result['scene']],'code'=>ERRNO['OK'],'msg'=>'ok']);
            }

        }else{
            return json(['msg'=>$result['msg'],'code'=>ERRNO['OTHERERR']]);
        }

    }

    public function register(){
        $data=$this->request->param();
        $this->validate($data,'app\api\validate\User.complete');
        $res=$this->userLogic->complete($data);
        if($res){
            $userInfo=$this->getResponseInfo($res);
            return json(['data'=>$userInfo,'code'=>ERRNO['OK'],'msg'=>'ok']);
        }else{
            return json(['msg'=>'complete error','code'=>ERRNO['OTHERERR']]);
        }
    }

    private function getResponseInfo($result){
        $userInfo['id']=$result['id'];
        $userInfo['username']=$result['username'];
        $userInfo['country']=0;

        $userInfo['type']=config('data.role_type.user');//1:商户2：用户
        $access_token=getToken($userInfo);
        $refresh_token=getToken($userInfo,2);
        $data=$userInfo;
        //获取用户头像
        if(!isset($result['avatar']) || empty($result['avatar'])){
            $data['avatar']=get_file_path(config('data.user_avatar_default')[0]);
        }else{
            $data['avatar']=get_file_path($result['avatar']);
        }
        $data['access_token']=$access_token;
        $data['refresh_token']=$refresh_token;
        //存session
        Session::clear();
        Session::set('userId',$result['id']);
//        Session::set('userName',$info['name']);
        Session::set('userType',config('data.role_type.user'));
//        Session::set('country',$result['country']);
        return $data;
    }
    /**
     * 完善用户信息
     */
    public function complete(){
        $data=$this->request->param();
        $this->validate($data,'app\api\validate\User.complete');
        if($this->userLogic->complete($data)){
            $data['id']=$data['user_id'];
            $userInfo=$this->userLogic->userInfo($data['user_id'],'country');
            $data['country']=$userInfo['country'];
            $response=$this->getResponseInfo($data);
            return json(['data'=>$response,'code'=>ERRNO['OK'],'msg'=>'ok']);
        }else{
            return json(['msg'=>'update error','code'=>ERRNO['OTHERERR']]);
        }
    }

    public function edit(){
        $data=$this->request->param();
        $this->validate($data,'app\api\validate\User.edit');
        if($this->userLogic->edit($data)){
            return json(['data'=>true,'code'=>ERRNO['OK'],'msg'=>'edit success']);
        }else{
            return json(['msg'=>'update error','code'=>ERRNO['OTHERERR']]);
        }
    }

    public function index(){
        $info=$this->userLogic->index();
        return json(['data'=>$info,'code'=>ERRNO['OK'],'msg'=>'ok']);
    }

    public function logout(){
        $this->userLogic->logout();
        return json(['data'=>true,'code'=>ERRNO['OK'],'msg'=>'ok']);
    }

    public function logoff(){
        $data=$this->request->param();
        $this->validate($data,'app\api\validate\User.logoff');
        $res=$this->userLogic->logoff($data);
        if($res['req']==1){
            return json(['data'=>true,'code'=>ERRNO['OK'],'msg'=>'ok']);
        }else{
            return json(['msg'=>$res['msg'],'code'=>ERRNO['OTHERERR']]);
        }
    }

    public function avatar(){
        $data=$this->request->param();
        $this->validate($data,'app\api\validate\User.avatar');
        if($this->userLogic->edit($data)){
            return json(['data'=>true,'code'=>ERRNO['OK'],'msg'=>'upload success']);
        }else{
            return json(['msg'=>'update error','code'=>ERRNO['OTHERERR']]);
        }
    }

    public function like(){
        $params['id']=Session::get('userId');
        $params['country']=Session::get('country');
        $params['page']=$this->request->post('page',1);
        $params['pageSize']=$this->request->post('pageSize',10);
        $data=$this->postsLogic->myLike($params);
        return json(['data'=>$data,'code'=>ERRNO['OK'],'msg'=>'ok']);
    }
}
